package com.example.homework.ui

import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.lifecycle.lifecycleScope
import androidx.transition.TransitionInflater
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.example.homework.databinding.FragmentPhotoBinding
import kotlinx.coroutines.launch


class PhotoFragment : Fragment() {
    private var transitionName: String? = null
    private var _binding: FragmentPhotoBinding? = null
    private val binding: FragmentPhotoBinding
        get() = _binding!!
    private var imgSrc: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val animation =
            TransitionInflater.from(requireContext()).inflateTransition(android.R.transition.move)
        sharedElementEnterTransition = animation
        sharedElementReturnTransition = animation
        arguments?.let {
            imgSrc = it.getString(ARG_IMG_SRC)
            transitionName = it.getString(ARG_IMG_TRANSITION)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        _binding = FragmentPhotoBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        transitionName?.let {
            ViewCompat.setTransitionName(binding.marsImageView, it)
        }
        postponeEnterTransition()

        lifecycleScope.launch {
            Glide.with(binding.marsImageView).load(imgSrc)
                .placeholder(android.R.drawable.progress_indeterminate_horizontal)
                .error(android.R.drawable.stat_notify_error)
                .listener(object : RequestListener<Drawable> {
                    override fun onResourceReady(
                        resource: Drawable,
                        model: Any,
                        target: Target<Drawable>?,
                        dataSource: com.bumptech.glide.load.DataSource,
                        isFirstResource: Boolean
                    ): Boolean {
                        startPostponedEnterTransition()
                        return false
                    }

                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>,
                        isFirstResource: Boolean
                    ): Boolean {
                        startPostponedEnterTransition()
                        return false
                    }


                }).into(binding.marsImageView)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        const val ARG_IMG_SRC: String = "imgSrc"
        const val ARG_IMG_TRANSITION: String = "transition_id"

        @JvmStatic
        fun newInstance(imgSrc: String, transition: String) = PhotoFragment().apply {
            arguments = Bundle().apply {
                putString(ARG_IMG_SRC, imgSrc)
                putString(ARG_IMG_TRANSITION, transition)
            }
        }
    }
}